package kz.pdt12.hw.tests;

import kz.pdt12.hw.data.GroupData;
import kz.pdt12.hw.utils.SortedListOf;
import org.testng.annotations.Test;

import java.util.Random;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 09.03.13
 * Time: 23:47
 */
public class GroupModificationTests extends TestBase {

    @Test(dataProvider = "randomValidGroupGenerator")
    public void modifySomeGroup(GroupData group) {
        // save old state
        SortedListOf<GroupData> oldList = app.getGroupHelper().getUiGroups();

        Random random = new Random();
        int index = random.nextInt(oldList.size() - 1);

        // actions
        app.getGroupHelper().modifyGroup(index, group);

        // save new state
        SortedListOf<GroupData> newList = app.getGroupHelper().getUiGroups();

        //compare states
        assertThat(newList, equalTo(oldList.without(index).withAdded(group)));

        if (wantToCheck()) {
            if (app.getPropertyKey("check.db").equals("yes")) {
                assertThat(app.getModel().getGroups(), equalTo(app.getHibernateHelper().listGroups()));
            }
            if (app.getPropertyKey("check.ui").equals("yes")) {
                assertThat(app.getModel().getGroups(), equalTo(app.getGroupHelper().getUiGroups()));
            }
        }
    }
}
